package com.ks.app.navigate.util;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

public class JsonUtil {

	/**
	 * 将obj用JSONObject来描述
	 * @param obj
	 * @return
	 */
	public static JSONObject object2JsonObject(Object obj) {
		JSONObject jsonObject = JSONObject.fromObject(obj);
		return jsonObject;
	}

	/**
	 * 将数组转为JSONArray格式
	 * @param objs
	 * @return
	 */
	public static JSONArray array2JsonArray(Object[] objs) {
		JSONArray jsonArray = JSONArray.fromObject(objs);
		return jsonArray;
	}

	/**
	 * 将Json格式的字符串转为Json数组  格式:("['json','is','easy']")
	 * @param text
	 * @return
	 */
	public static JSONArray jsonText2JsonArray(String text) {
		JSONArray jsonArray = JSONArray.fromObject(text);
		return jsonArray;
	}
	
	/**
	 * 将Json格式的字符串转为Json对象  格式:("{'json','is','easy'}")
	 * @param text
	 * @return
	 */
	public static JSONObject jsonText2JsonObject(String text) {
		JSONObject jsonObject = JSONObject.fromObject(text);
		return jsonObject;
	}

	/**
	 * 将List集合转为JSONArray格式
	 * @param list
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static JSONArray list2JsonArray(List list) {
		JSONArray jsonArray = JSONArray.fromObject(list);
		return jsonArray;
	}

	/**
	 * 将Map集合转为Json格式数据
	 * @param map
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static JSONObject map2JsonObject(Map map) {
		JSONObject json = JSONObject.fromObject(map);
		return json;
	}

	/**
	 * 将json对象转换成实体bean
	 * @param json
	 * @param c
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Object jsonObject2Bean(String json, Class c) {
		JSONObject jb = JSONObject.fromObject(json);
		return JSONObject.toBean(jb, c);
	}

	/**
	 * 输出JSON格式数据
	 * @param obj 
	 * @param response
	 * @throws IOException
	 * @see 参考方法
	 */
	@SuppressWarnings("unchecked")
	public static void outJson(Object obj, HttpServletResponse response,String format) {
		if (obj == null)
			return;
		String result = null;
		try {
			
			if (obj instanceof List) {
				result = list2JsonArray((List) obj).toString();
			} else if (obj instanceof Map) {
				
				JsonValueProcessor jsonProcessor = new DateJsonValueProcessor(format); 
		        JsonConfig jsonConfig = new JsonConfig(); 
		        //注册值处理器 
		        jsonConfig.registerJsonValueProcessor(Date.class, jsonProcessor); 
		        result = JSONSerializer.toJSON(obj , jsonConfig).toString(); 
		        
			} else {
				result = object2JsonObject(obj).toString();
			}

			ActionResponseUtils.wirteAndEnd(result, response);
		} catch (Exception e) {
			//log.error("输出JSON错误",e);
			e.printStackTrace();
		}

	}
	
	/**
	 * 输出JSON格式数据
	 * @param obj 
	 * @param response
	 * @throws IOException
	 * @see 参考方法
	 */
	@SuppressWarnings("unchecked")
	public static void outJson(Object obj, HttpServletResponse response) {
		if (obj == null)
			return;
		String result = null;
		try {
			
			if (obj instanceof List) {
				result = list2JsonArray((List) obj).toString();
			} else if (obj instanceof Map) {
				
				JsonValueProcessor jsonProcessor = new DateJsonValueProcessor(); 
		        JsonConfig jsonConfig = new JsonConfig(); 
		        //注册值处理器 
		        jsonConfig.registerJsonValueProcessor(Date.class, jsonProcessor); 
		        result = JSONSerializer.toJSON(obj , jsonConfig).toString(); 
		        
			} else {
				result = object2JsonObject(obj).toString();
			}

			ActionResponseUtils.wirteAndEnd(result, response);
		} catch (Exception e) {
			//log.error("输出JSON错误",e);
			e.printStackTrace();
		}

	}

	@SuppressWarnings("unchecked")
	public static String object2JsonText(Object obj) {
		if (obj == null)
			return null;
		String result = null;
		try {
			if (obj instanceof List) {
				result = list2JsonArray((List) obj).toString();
			} else if (obj instanceof Map) {
				result = map2JsonObject((Map) obj).toString();
			} else {
				result = object2JsonObject(obj).toString();
			}
			/*JsonConfig config = new JsonConfig();
			config.setIgnoreDefaultExcludes(false);
			config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
			result = JSONSerializer.toJSON(obj, config).toString();
			ActionResponseUtils.wirteAndEnd(result, response);*/
			return  result ;
			
		} catch (Exception e) {
			//log.error("输出JSON错误",e);
			e.printStackTrace();
			return null;
		}

	}
}
